﻿using System;
using eShop.Business;
using System.Collections.Generic;
using Ext.Net;
using eShop.Model;

public partial class admin_product_list : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
            LoadProductCategory();
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindProducts(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    private void BindProducts(string sort, int start, int limit)
    {
        List<Product> data = null;
        int totalCount = 0;
        if (String.IsNullOrEmpty(sort))
            sort = "ProductId";

        if (this._useSearch)
        {

            string _searchProductName = txtToolbarSearch.Text.Trim();
            string _itemNumber = txtToolbarSearchItemNumber.Text.Trim();
            int _parentCategoryId;

            Int32.TryParse(ddlToolbarCategories.SelectedItem.Value, out _parentCategoryId);
            if (_parentCategoryId == SiteUtility.SuperRootCategoryId)
                _parentCategoryId = 0;
            //_parentCategoryId = SiteUtility.SuperRootCategoryId;

            data = ProductManager.GetBackEndProductListBySearchAdmin(_searchProductName, _itemNumber, _parentCategoryId, false, sort, start, limit);
            totalCount = ProductManager.GetBackEndProductListBySearchCountAdmin(_searchProductName, _itemNumber, _parentCategoryId, false);

            //Session["_useSearch"] = null;

        }
        else
        {
            data = ProductManager.GetBackEndProductListBySearch("", 0, false, sort, start, limit);
            totalCount = ProductManager.GetBackEndProductListBySearchCount("", 0, false);
        }

        Store1.DataSource = data;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    protected void LoadProductCategory()
    {
        List<Category> productCategoryTree = CategoryManager.GetRootCategories();
        foreach (Category productCategory in productCategoryTree)
        {
            ddlToolbarCategories.Items.Add(new Ext.Net.ListItem(productCategory.CategoryName, productCategory.Id.ToString()));
        }
    }


    [DirectMethod(Namespace = "EShop.WebService")]
    public void DeleteProducts(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        ProductManager.DeleteProducts(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "EShop.WebService")]
    public void SearchProduct()
    {
        _useSearch = true;
        PagingToolbar1.SetPageIndex(1);
        Store1.DataBind();
    }

}